home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16936 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  63 lines

  1. Path: nntpa.cb.att.com!ssr
  2. From: ssr@qc56.ho.att.com (BL0314500-S.RAVIKUMAR(HOH167)NONE)
  3. Newsgroups: att.lang.c++,comp.lang.c++
  4. Subject: Help with STL set container - Assigning a set of dervied to a set of base
  5. Date: 12 Apr 1996 20:08:38 GMT
  6. Organization: AT&T Bell Laboratories
  7. Distribution: world
  8. Message-ID: <SSR.96Apr12160838@qc56.ho.att.com>
  9. NNTP-Posting-Host: qc56.ho.att.com
  10.  
  11.  
  12. The following program results in a memory fault at the assignment
  13. point. Can someone show me how to fix it. BTW, how to implement set of
  14. uninterpreted pointers in STL.
  15.  
  16. Thanks.
  17.  
  18. -- 
  19. ravi
  20. ssr@hoserve.att.com
  21.  
  22. #include <stl_set.h>
  23. #include <algo.h>
  24. #include <assert.h>
  25. #include <string.h>
  26.  
  27. struct Base
  28. {
  29.   char _id[3];
  30.   Base(const char* id) { strcpy(_id, id); }
  31. };
  32.  
  33. struct Derived : public Base
  34. {
  35.   char _name[3];
  36.   Derived(const char* id, const char* name) : Base(id) { strcpy(_name, name); }
  37. };
  38.  
  39.  
  40. int
  41. main()
  42. {
  43.   set< Base*, less<Base*> > bs;
  44.   bs.insert(new Base("b1"));
  45.   bs.insert(new Base("b2"));
  46.  
  47.   set< Derived*, less<Derived*> > ds;
  48.   ds.insert(new Derived("BB", "d1"));
  49.   ds.insert(new Derived("BB", "d2"));
  50.  
  51.   cout << "Assigning derived set to base set" << endl;
  52.  
  53.   //  >>>>>>>>> memory fault line >>>>>>>>>>
  54.   set< Base*, less<Base*> > bs1 = (set< Base*, less<Base*> >&)ds;
  55.  
  56.   cout << "Assignment succeeded" << endl;
  57.  
  58.   exit(0);
  59. }
  60. --
  61. ravi
  62. ssr@hoserve.att.com
  63.